package com.xyht.sca_s.student_manage_system.modules.student.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgStructure;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgStructureMapper;
import com.xyht.sca_s.student_manage_system.modules.student.entity.req.SmsStudentExportReq;
import com.xyht.sca_s.student_manage_system.modules.student.entity.req.SmsStudentInfoReq;
import com.xyht.sca_s.student_manage_system.modules.student.service.SmsStudentInfoService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 学生学籍信息表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2022-12-03
 */
@RestController
@RequestMapping("/student/")
@SecretBody
public class SmsStudentInfoController {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private SmsStudentInfoService smsStudentInfoService;
    @Autowired
    private SmsOrgStructureMapper smsOrgStructureMapper;

    @ApiModelProperty(value = "批量导入")
    @PostMapping("/batch/add")
    public ResponseResult batchImportStudentInfo(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsStudentInfoService.batchImportStudentInfo(map, user_id);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiModelProperty(value = "学籍添加")
    @PostMapping("/add")
    public ResponseResult addStudentInfo(@RequestBody SmsStudentInfoReq smsStudentInfoReq) {
        return smsStudentInfoService.addStudentInfo(smsStudentInfoReq);
    }

    @ApiModelProperty(value = "学籍组织树")
    @GetMapping("/orgTree")
    public ResponseResult getOrgTree() {
        return smsStudentInfoService.getOrgTree();
    }

    @ApiModelProperty(value = "学籍列表")
    @GetMapping("/list")
    public ResponseResult getStudentInfoList(@RequestParam(value = "org_id",required = false) Integer org_id,
                                             @RequestParam(value = "stuCollage",required = false) String stuCollage,
                                             @RequestParam(value = "stuMajor",required = false) String stuMajor,
                                             @RequestParam(value = "stuGrade",required = false) String stuGrade,
                                             @RequestParam(value = "stuClass",required = false) String stuClass,
                                             @RequestParam(value = "stuStatus",required = false) Integer stuStatus,
                                             @RequestParam(value = "searchStr",required = false) String searchStr,
                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return smsStudentInfoService.getStudentInfoList(org_id, stuCollage, stuMajor, stuGrade,
                stuClass, stuStatus, searchStr, pageNum, pageSize);
    }

    @ApiModelProperty(value = "下拉框内容")
    @GetMapping("/dropBox")
    public ResponseResult getAddStudentDropBoxContent(
            @RequestParam(value = "type",required = false)Integer type,
            @RequestParam(value = "id",required = false)Integer id) {
        return smsStudentInfoService.getAddStudentDropBoxContent(type, id);
    }

    @ApiModelProperty(value = "批量删除")
    @PostMapping("/batch/delete")
    public ResponseResult batchDeleteStudentInfo(@RequestBody Map<String,Object> map) {
        return smsStudentInfoService.batchDeleteStudentInfo(map);
    }

    @ApiModelProperty(value = "学籍详情")
    @GetMapping("/detail")
    public ResponseResult getStudentInfoDetail(@RequestParam("stu_id") String stu_id) {
        return smsStudentInfoService.getStudentInfoDetail(stu_id);
    }

    @ApiModelProperty(value = "修改学籍信息")
    @PostMapping("/update")
    public ResponseResult updateStudentInfo(@RequestBody SmsStudentInfoReq smsStudentInfoReq) {
        return smsStudentInfoService.updateStudentInfo(smsStudentInfoReq);
    }

    @ApiModelProperty(value = "批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportStudentInfo(@RequestBody SmsStudentExportReq smsStudentExportReq, HttpServletResponse response) {
        SmsOrgStructure orgStructure;
        if (smsStudentExportReq.getOrg_id() != null) {
            orgStructure = smsOrgStructureMapper.selectById(smsStudentExportReq.getOrg_id());
            if (orgStructure == null) {
                return CommonResult.failed(CommonCodeEnum.ORG_NOT_EXIST);
            }
        }
        try{
            Future<String> future = smsStudentInfoService.exportStudentInfo(smsStudentExportReq, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @ApiModelProperty(value = "学籍下载")
    @GetMapping("/downloadStatusInfo")
    public ResponseResult downloadStudentStatusInfo(HttpServletRequest request,HttpServletResponse response) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsStudentInfoService.exportStudentStatus(user_id,request,response);
    }


}

